package uk.ac.tess.timemanagement.todolist;


import uk.ac.tess.timemanagement.MainActivity;
import uk.ac.tess.timemanagement.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class ToDoActionBar extends Activity {
	
	private Menu myMenu;

	public boolean onCreateOptionsMenu(Menu menu) {
		myMenu = menu;
        MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.to_do_action_bar, myMenu);
		this.showIconsInBar(false);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			Intent intent = new Intent(this, MainActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.todo_action_add:
			this.showIconsInBar(true);
			setContentView(R.layout.todo_new_layout);
			// maybe change to a new activity
			EditText edit = (EditText) findViewById(R.id.todo_new_description);
			Log.d("Log","Focused: " + edit.isFocused());
			InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
			imm.showSoftInput(edit, InputMethodManager.SHOW_IMPLICIT);
			return true;
		case R.id.todo_action_save:
			TextView description = (TextView) findViewById(R.id.todo_new_description);
			CheckBox isActive = (CheckBox) findViewById(R.id.todo_new_active);
			TextView dateTime = (TextView) findViewById(R.id.todo_new_datetime_text);
			String sDateTime = dateTime.getText().toString();
			ToDoDatabaseHandler db = new ToDoDatabaseHandler(getApplicationContext());
			db.addToDoItem(new ToDoItem(isActive.isChecked(), description.getText().toString(), parseDate(sDateTime), parseTime(sDateTime)));
			startActivity(new Intent(getApplicationContext(),ToDoMain.class));
			return true;
		case R.id.todo_action_discart:
			startActivity(new Intent(getApplicationContext(),ToDoMain.class));
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void selectDateTodoHandler(View v){
		Log.d("Log","Take to callendar");
	}
	
	private void showIconsInBar(boolean show){
		myMenu.findItem(R.id.todo_action_save).setVisible(show);
		myMenu.findItem(R.id.todo_action_discart).setVisible(show);
		myMenu.findItem(R.id.todo_action_add).setVisible(!show);
	}
	
	private String parseDate(String datetime){
		return "Test Date";
	}
	
	private String parseTime(String datetime){
		return "Test Time";
	}
}
